//获取应用实例
const app = getApp();

Page({
	data: {
    nickname:'',
    avatar:'',//头像
    collectNum:0,
    isLogin:true,
    avatar:'',
    phone:'',
    auth_code:'',
    default_avatar:'',
    user_msg:[],
  },
	onLoad: function(options) {
    // this.getUserMsg();
    // if(options.auth_code){
    //   wx.setStorageSync("auth_code",options.auth_code)
    // }else{
    //   wx.removeStorageSync("auth_code")
    // }
  },

  onShow(){
    if(app.globalData.token!=""){
      this.setData({
        isLogin: true
      })
      this.getUserMsg();
    }else{
      this.setData({
        isLogin:false,
        avatar: app.globalData.avatar//使用默认头像
      })
      wx.navigateTo({
        url: '/pages/mine/login',
      })
    }
  },
  onShareAppMessage: function (e) {
    if (e.from === 'button') {
      console.log("来自页面内转发按钮");
        return {
          title: '快来汇课熊小程序领取优惠券啦',
          desc: '',
          imageUrl: "/static/images/splash.png",
          path: '/pages/live/splash?auth_code='+app.globalData.auth_code // 路径，传递参数到指定页面。
        }
    }
  },                                                                                                                       

downloadImg:function(){
  if(app.globalData.alipay_account1){
    wx.navigateTo({
      url: '/pages/mine/wallet',
    })
  }else{
    wx.navigateTo({
      url: '/pages/mine/setting-bindaliay',
    })
  }
 
},
  toShared:function(){
    wx.navigateTo({
      url: '/pages/mine/shared',
    })
  },
  //获取用户信息
  getUserMsg(){
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=User&a=getUserMsg',
      data: {
        token: app.globalData.token
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        if(res.data.code==0){
          that.setData({
            user_msg:res.data.data.user_msg,
            user_detail:res.data.data.user_detail,
            avatar:res.data.data.user_detail.avatar
          })
          app.globalData.balance=res.data.data.user_msg.balance

          app.globalData.nickname = res.data.data.user_detail.nickname,
          app.globalData.weixinid = res.data.data.user_detail.weixin,
          app.globalData.avatar=res.data.data.user_detail.avatar
          app.globalData.balance_pdd = res.data.data.user_msg.balance_pdd
          app.globalData.truename = res.data.data.user_detail.truename

          app.globalData.alipay_account=res.data.data.user_msg.alipay_account

          app.globalData.auth_code=res.data.data.user_msg.auth_code
        }else{
          if(res.data.code==105){
            wx.removeStorage({
              key: 'token',
              success: function (res) {
                console.log(res)
              },
            })
            app.globalData.token='';
            wx.reLaunch({
              url: '/pages/mine/login',
            })
          }
          wx.showToast({
            title: ''+res.data.msg,
          })
        }
      }, fail: function () {

      },complete:function(){
        wx.hideLoading();
      }
    });
  },

  login:function(){
    wx.reLaunch({
      url: 'login',
    })
  },
  tocash:function(){
    wx.showToast({
      title: '请去汇客熊app进行提现',
      icon:'none'
    })
  },

  copyText:function(e){
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
toset:function(){
  var that=this;
  // wx.navigateTo({
  //   url: '/pages/mine/setting',
  // })
  wx.showModal({
    title: '提示',
    content: '确认要退出当前账号吗?',
    success: function (res) {
      console.log(res)
      if (res.confirm) {
        that.logout();
      } else {
       
      }
    }
  })
},
  //退出登录
  logout: function () {
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=User&a=loginout',
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      data: {
        'token': '' + app.globalData.token
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 0) {
          wx.removeStorage({
            key: 'token',
            success: function (res) {
              console.log(res)
            },
          })
          wx.removeStorage({
            key: 'uid',
            success: function (res) {
              console.log(res)
            },
          })
          app.globalData.token = '';
          app.globalData.uid = "";
          wx.reLaunch({
            url: '/pages/mine/login',
          })
        } else {
          wx.showToast({
            title: '' + res.data.msg,
            icon: "none"
          })
        }
      },
    });
  },
  upPersondata:function(){
    wx.navigateTo({
      url: 'person-data',
    })
  },
  copyAuth: function (e) {
    console.log(e)
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
});
